package com.dg.gaming.api.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.DataFormatException;

/**
 * Couple of String gzipping utilities.
 * @author Scott McMaster
 *
 */
public class StringZipper {

    /**
     * Gzip the input string into a byte[].
     * @param input
     * @return
     * @throws IOException
     */
    public static byte[] zipStringToBytes(String str) throws IOException {
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        BufferedOutputStream bufos = new BufferedOutputStream(new
//                GZIPOutputStream(bos));
//        bufos.write(input.getBytes());
//        bufos.close();
//        byte[] retval = bos.toByteArray();
//        bos.close();
//        return retval;

        byte[] input = str.getBytes("UTF-8");

        // Create the compressor with highest level of compression
        Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.BEST_COMPRESSION);

        // Give the compressor the data to compress
        compressor.setInput(input);
        compressor.finish();

        // Create an expandable byte array to hold the compressed data.
        // You cannot use an array that's the same size as the orginal because
        // there is no guarantee that the compressed data will be smaller than
        // the uncompressed data.
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        // Compress the data
        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        try {
            bos.close();
        } catch (IOException e) {
        }

        // Get the compressed data
        byte[] compressedData = bos.toByteArray();
        return compressedData;
    }

    /**
     * Unzip a string out of the given gzipped byte array.
     * @param bytes
     * @return
     * @throws IOException
     */
    public static String unzipStringFromBytes(byte[] compressedData) throws
            IOException {
//        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
//        BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(
//                bis));
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        byte[] buf = new byte[1024];
//        int len;
//        while ((len = bufis.read(buf)) > 0) {
//            bos.write(buf, 0, len);
//        }
//        String retval = bos.toString("UTF8");
//        retval = retval.trim();
//        bis.close();
//        bufis.close();
//        bos.close();
//        return retval;
        // Create the decompressor and give it the data to compress
        Inflater decompressor = new Inflater();
        decompressor.setInput(compressedData);

        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.
                length);

        // Decompress the data
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
            }
        }
        try {
            bos.close();
        } catch (IOException e) {
        }

        // Get the decompressed data
        byte[] decompressedData = bos.toByteArray();
        return new String(decompressedData, "UTF8");
    }

    /**
     * Static class.
     *
     */
    private StringZipper() {
    }
}
